#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""  
@Project : pythonProject
@File : ssh_val.py
@Author :周浩然
@Time : 2024/10/6 16:42  
@脚本说明 : 
"""

import paramiko
def scan_ssh(host, port=22, username="root", password="root"):
    try:
        # 1. 构建 tcp 传输通道 生成 transport 连接语句柄
        transport = paramiko.Transport((host, port))
        # 2. 建立 ssh 连接配置 需要配置账号 密码
        transport.connect(username=username, password=password)
        # 3. 解决 banner 报错
        transport.banner_timeout = 60
        # 4. 创建 ssh 通道 创建 ssh客户端
        ssh_client = paramiko.SSHClient()
        if ssh_client:
            print(f"[+] SSH连接成功: {username} / {password}")
            print(f"[+]redis{host}:{port}存在弱口令，{username}:{password}")
            with open(file="./redis_results.txt", mode="a+", encoding="utf-8") as f:
                f.write(f"[+] redis{host}:{port}存在弱口令，{username}:{password}\n")
            # 5. 关闭通道
            ssh_client.close()
            transport.close()
    except Exception as e:
        print(f"[-] SSH连接失败: {username} / {password}")

def val_ssh(host, port, user, file):
    try:
        with open(file="./user.txt", mode="r", encoding="utf-8") as f1:
            for user in f1:
                with open(file=f"{file}", mode="r", encoding="utf-8") as f2:
                    for passwd in f2:
                        scan_ssh(host="192.168.209.66", username=user.strip(), password=passwd.strip())
    except:
        print("[-] 读取弱口令文件出错")

